//
//  WYLeftBarButtonItem.m
//  Boss
//
//  Created by admin on 2018/6/7.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYLeftBarButtonItem.h"
#import "WYShadowButton.h"
@interface WYLeftBarButtonItem()
@property (nonatomic,strong)UILabel *label;
@end



@implementation WYLeftBarButtonItem

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews
{
    
   //self.backgroundColor = [UIColor purpleColor];
    
    WYShadowButton *backgroundImageView = [[WYShadowButton alloc]init];
    backgroundImageView.userInteractionEnabled = NO;
    [self addSubview:backgroundImageView];
    [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.right.mas_equalTo(0);
      //  make.width.mas_equalTo(self.width);
    }];
    
    [backgroundImageView setImage:[UIImage imageNamed:@"leftBarButtonItemView"] forState:UIControlStateNormal];
    
    
//    backgroundImageView.image = [UIImage imageNamed:@"left_backImage"];
    
    UIImageView *leftImageView = [[UIImageView alloc]init];
    [self addSubview:leftImageView];
    [leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(26);
    }];
    leftImageView.image = [UIImage imageNamed:@"redPacket_icon"];
    
    
    UIImageView *rightImageView = [[UIImageView alloc]init];
    [self addSubview:rightImageView];
    [rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
//        if (@available(iOS 11, *)) {
//            make.right.mas_equalTo(0);
//
//        }else{
            make.right.mas_equalTo(-15);
     //  }
        make.width.height.mas_equalTo(22);
    }];
    rightImageView.image = [UIImage imageNamed:@"icon_add"];
    
    UILabel *label = [[UILabel alloc]init];
    self.label  = label;
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.left.equalTo(leftImageView.mas_right).offset(0);
        make.right.equalTo(rightImageView.mas_left).offset(0);
    }];
    
    label.textAlignment = NSTextAlignmentCenter;
    label.font = kFontSystemBIgger2(13);
    
    NSString *lenth = @" 3 ";
    
    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@" 还能抢%@次 ",lenth]];
    label.textColor = [UIColor hexStringToColor:@"#535353" alpha:1.0];
  //  label.textColor = [UIColor greenColor];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor hexStringToColor:@"#d8593f" alpha:1.0] range:NSMakeRange(3, lenth.length)];
    [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:NSMakeRange(3, lenth.length)];
   // label.text = @"还能抢 5 次";
    label.attributedText= aString;
    
    self.addButton  = [[UIButton alloc]init];
    [self addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    
}
-(void)setLabelText:(NSString *)labelText
{
    _labelText =labelText;
    if (labelText.integerValue>99) {
        labelText = @"99";
        labelText = [NSString stringWithFormat:@" %@+ ",labelText];
    }else{
        labelText = [NSString stringWithFormat:@" %@ ",labelText];
    }
    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"还能抢%@次",labelText]];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor hexStringToColor:@"#d8593f" alpha:1.0] range:NSMakeRange(3, labelText.length)];
    [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:NSMakeRange(3, labelText.length)];
    self.label.attributedText = aString;
}

@end
